var pages = 1;
var tpnum = 0;
var nowSearchWord = decodeURI(getParam('key'));
$(function() {
	//搜索跳转
	$("#myInput").bind("search", function() {
		var newWord = $('#myInput').val();
		nowSearchWord=newWord;
		loginSearch({
		pages: 1,
		userUuid: CD.geturl().business,
		goodsName: nowSearchWord,
		sort:""
	}, function(data) {
		$('#searchMore').html(template('searchTemplate', data));
		searchOfProduct();
		pages = 1;
		searchUp(pages, nowSearchWord);
	});
	});

	
	//排序点击
	$('.ld_order a').on('tap', function() {
		/*当前点击的A*/
		var $this = $(this);
		/*如果之前没有选择*/
		if(!$this.hasClass('now')) {
			/*选中，其他的不选中，箭头默认朝下*/
			$this.addClass('now').siblings().removeClass('now')
				.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
		}
		/*有now的时候*/
		else {
			/*改当前的箭头方向*/
			if($this.find('span').hasClass('fa-angle-down')) {
				$this.find('span').removeClass('fa-angle-down').addClass('fa-angle-up');
			} else {
				$this.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
			}
		}
	});

	$('#myInput').val(nowSearchWord);
	loginSearch({
		pages: 1,
		userUuid: CD.geturl().business,
		goodsName: nowSearchWord,
		sort:""
	}, function(data) {
		$('#searchMore').html(template('searchTemplate', data));
		searchOfProduct();
		pages = 1;
		searchUp(pages, nowSearchWord);
	});

	//价格排序
	$('#price-collection').on('tap', function() {
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0, 0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			tpnum = 2;
			loginSearch({
				pages: 1,
				userUuid: CD.geturl().business,
				goodsName: nowSearchWord,
				sort:tpnum
			}, function(data) {
				$('#searchMore').html(template('searchTemplate', data));
				searchOfProduct();
				pages = 1;
				searchUp(pages, nowSearchWord);
			});
		} else {
			tpnum = 3;
			loginSearch({
				pages: 1,
				userUuid: CD.geturl().business,
				goodsName: nowSearchWord,
				sort:tpnum
			}, function(data) {
				$('#searchMore').html(template('searchTemplate', data));
				searchOfProduct();
				pages = 1;
				searchUp(pages, nowSearchWord);
			});
		}
	});
	
	//时间排序
	$('#time-collection').on('tap', function() {
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0,0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			tpnum = 0;
			loginSearch({
				pages: 1,
				userUuid: CD.geturl().business,
				goodsName: nowSearchWord,
				sort:tpnum
			}, function(data) {
				$('#searchMore').html(template('searchTemplate', data));
				searchOfProduct();
				pages = 1;
				searchUp(pages, nowSearchWord);
			});
		} else {
			tpnum = 1;
			loginSearch({
				pages: 1,
				userUuid: CD.geturl().business,
				goodsName: nowSearchWord,
				sort:tpnum
			}, function(data) {
				$('#searchMore').html(template('searchTemplate', data));
				searchOfProduct();
				pages = 1;
				searchUp(pages, nowSearchWord);
			});
		}
	});

})
//进入搜索
var loginSearch = function(params, callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		url: window.address + "/goods/findDimBayUuid",
		data: params,
		dataType: "json",
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
};

//汉字转化 
function getParam(paramName) {   
	paramValue = "", isFound = !1;   
	if(this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {     
		arrSource = unescape(this.location.search).substring(1,
			this.location.search.length).split("&"), i = 0;     
		while(i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 &&
			arrSource[i].split("=")[0].toLowerCase() ==                 paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++       
	}   
	return paramValue == "" && (paramValue = null), paramValue 
} ;

//上拉加载
var searchUp = function() {
	mui.init({
		pullRefresh: {
			container: "#mui-scroll-wrapper",
			up: {
				callback: function() {
					var that = this;
					pages++;
					loginSearch({
						"pages": pages,
						"sort": tpnum,
						"userUuid": CD.geturl().business,
						"goodsName": nowSearchWord
					}, function(data) {
						$('#searchMore').append(template('searchTemplate', data));
						searchOfProduct();
						console.log(nowSearchWord,8888);
						if(data.rows.length) {
							that.endPullupToRefresh();
						} else {
							that.endPullupToRefresh(true);
						}
					})
				}
			}
		}
	});
} 

var searchOfProduct = function(){
	$('.productList').on('tap',function(){
		var thisId = $(this).attr('data-id');
		window.location.href="v-shop-product.html?id="+thisId+'&business='+CD.geturl().business;	
	});
}
